home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1192 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  5.1 KB

  1. Path: gate.net!pslfl2-14
  2. From: bhutto@gate.net (William Hutto)
  3. Newsgroups: alt.msdos.programmer,comp.lang.c
  4. Subject: Re: Some C problems
  5. Date: 11 Jan 1996 23:37:09 GMT
  6. Organization: CyberGate, Inc.
  7. Message-ID: <4d46v5$1vs0@news.gate.net>
  8. References: <4d0fjj$eok@lugb.latrobe.edu.au>
  9. NNTP-Posting-Host: pslfl2-14.gate.net
  10. X-Newsreader: News Xpress Version 1.0 Beta #4
  11.  
  12. In article <4d0fjj$eok@lugb.latrobe.edu.au>,
  13.    cs102238@lux.latrobe.edu.au (Gregary John Boyles ) wrote:
  14. >PROBLEM 1.
  15. >void WriteInFileToOutFile(FILE *InFile,FILE *OutFile,char *InFileName,char 
  16. *OutFileName,long NumBytes,byte DiskNum)
  17. >{
  18. >     const word Size=1;
  19. >     const word MaxBytes=65535; Produces warning : conversion may lose 
  20.                                                     ^^^^^^^^^^
  21.  
  22. You better check this out. The compiler is telling you what it's doing. If 
  23. necessary just change to:
  24.  
  25. const unsigned MaxBytes;
  26.  
  27. >                                significant bits. Why? 65535 is within the
  28. >                                range for a word (unsigned int)
  29. >
  30. >     word NumRead,NumWritten;
  31. >     long Index;
  32. >     void *BufferPtr;
  33. >
  34. >     /* Write the disk number to the first byte of the file. */
  35. >     fwrite(&DiskNum,Size,1,OutFile);
  36. >     BufferPtr=(void *)malloc(MaxBytes);
  37.  
  38. On my machine, even with short test code, I wouldn't think of allocating 65535 
  39. bytes without checking for success:
  40.  
  41.     if(BufferPtr==NULL)
  42.         /*error*/
  43.  
  44. >     Index=0;
  45. >     while (Index<=NumBytes)
  46. >     {
  47. >
  48. >
  49. >Am I using fread and fwrite correctly or do I need to dereference bufferptr 
  50. >when I pass it to these procedures because I am finding that NumRead 
  51. >(number of bytes read) != NumWritten (number of bytes written - 0).
  52. <snip>
  53.  
  54. If you checked for success when opening these files the same way you did with 
  55. the malloc() function above (no checking at all), I would imagine that OutFile 
  56. either didn't open successfully or it wasn't opened in a write mode. Test the 
  57. pointers that these functions (malloc() and fopen() [assuming you used 
  58. fopen()]) return against NULL.
  59.  
  60.  
  61. >
  62. >*****************************************************************************
  63. *
  64. >*                                                                            
  65. *
  66. >*         NumRead=(fread(BufferPtr,Size,MaxBytes,InFile))*Size;              
  67. *
  68. >*         if (NumRead==0)                                                    
  69. *
  70. >*         {                                                                  
  71. *
  72. >*              strcpy(ErrorMessage,"An error occured while reading from 
  73. infil*
  74. >*              strcat(ErrorMessage,InFileName);                              
  75. *
  76. >*              strcat(ErrorMessage," - please make sure that this file is 
  77. not*
  78. >*              Error(ErrorMessage);                                          
  79. *
  80. >*              fclose(OutFile);                                              
  81. *
  82. >*              fclose(InFile);                                               
  83. *
  84. >*              exit(1);                                                      
  85. *
  86. >*         }                                                                  
  87. *
  88. >*         NumWritten=(fwrite(BufferPtr,Size,NumRead,OutFile))*Size;          
  89. *
  90. >*         if (NumWritten!=NumRead)                                           
  91. *
  92. >*         {                                                                  
  93. *
  94. >*              strcpy(ErrorMessage,"An error occured while writing to 
  95. outfile*
  96. >*              strcat(ErrorMessage,OutFileName);                             
  97. *
  98. >*              strcat(ErrorMessage," - number of bytes read does not equal 
  99. th*
  100. >*              Error(ErrorMessage);                                          
  101. *
  102. >*              fclose(OutFile);                                              
  103. *
  104. >*              fclose(InFile);                                               
  105. *
  106. >*              exit(1);                                                      
  107. *
  108. >*         }                                                                  
  109. *
  110. >*****************************************************************************
  111. *
  112. >
  113. >          Index+=NumRead;
  114. >     }
  115. >}
  116. >
  117. >
  118. >
  119. >With this statement DiskInfo.df_total*DiskInfo.df_sclus*DiskInfo.df_bsec 
  120. calculated
  121. >using my calculator produces the correct drive capacity i.e. 360K however
  122. >DriveSize does not contain the correct value i.e. 32000 instead of 360000 
  123. (approximate).
  124. >Why is this happening? The above fields are of type unsigned.
  125. >
  126. >PROBLEM 2.
  127. >long DriveSize;
  128. >struct dfree DiskInfo;
  129. >..
  130. >..
  131. >..
  132. >
  133. >getdfree(DriveNum,&DiskInfo);
  134. >DriveSize=DiskInfo.df_total*DiskInfo.df_sclus*DiskInfo.df_bsec;
  135. >
  136. >df_total:clusters
  137. >df_sclus:sectors/cluster
  138. >df_bsec:bytes/sector
  139.  
  140. This can be found in a good debugging session. You have to trace down where 
  141. this value is coming from. 
  142.  
  143. >
  144. >
  145. >PROBLEM 3.
  146. >When I want to output a long int type varaible with printf, it prints out
  147. >a garbage value for the variable despite declaring it as a li/ld in the
  148. >format string. What am I doing wrong?
  149.  
  150. Define a garbage value.
  151.  
  152. >
  153. >PROBLEM 4.
  154. >How do you use literal constants bigger than words e.g. how would you use
  155. >the literal constant 1000000 without getting the 'constant out of range'
  156. >warning/error?
  157.  
  158. 1000000L
  159.  
  160. Bill
  161.  
  162. "Whatcha got on?...Your mind?"
  163.